import { getCampusSelector } from '@/api/campus/campus'
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      isEdit: false,
      dataForm: {
        id: '',
        name: undefined,
        phone: undefined,
        brand: undefined,
        method: undefined,
        amount: undefined,
        time: undefined,
        type: undefined,
        managerId: undefined,
        isFinish: false,
        status: undefined,
        area: [],
        campusName: undefined,
        description: undefined,
        campusId: undefined,
        policy: undefined
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入客户名称',
            trigger: 'blur'
          }
        ],
        area: [
          {
            validator: (rule, value, callback) => {
              if (this.dataForm.type === '3' && !value) {
                return callback(new Error('所属区域为必填项'))
              } else {
                return callback()
              }
            },
            trigger: 'blur'
          }
        ],
        phone: [
          {
            required: true,
            message: '请输入联系方式',
            trigger: 'blur'
          },
          {
            pattern: /^1[3456789]\d{9}$|^0\d{2,3}-?\d{7,8}$/,
            message: '请输入正确的联系方式',
            trigger: 'blur'
          }
        ],
        brand: [
          {
            required: true,
            message: '请输入收款品牌',
            trigger: 'change'
          }
        ],
        method: [
          {
            required: true,
            message: '请输入收款方式',
            trigger: 'change'
          }
        ],
        amount: [
          {
            required: true,
            message: '请输入收款金额',
            trigger: 'blur'
          },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        time: [
          {
            required: true,
            message: '请输入收款时间',
            trigger: 'change'
          }
        ],
        type: [
          {
            required: true,
            message: '请输入收款类型',
            trigger: 'change'
          }
        ],
        campusId: [
          {
            validator: (rule, value, callback) => {
              if (this.dataForm.type === '2' && !value) {
                return callback(new Error('请输入收款校区'))
              } else {
                return callback()
              }
            },
            trigger: 'change'
          }
        ],
        managerId: [
          {
            required: true,
            message: '请输入招商经理',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请输入',
            trigger: 'change'
          }
        ]
      },
      brandOptions: [],
      campusOptions: [],
      campusLoading: false,
      methodOptions: [{ fullName: '刷卡', id: '1' }, { fullName: '转账', id: '2' }, { fullName: '扫码', id: '3' }, { fullName: '现金', id: '4' }],
      typeOptions: [{ fullName: '续约', id: '2' }, { fullName: '代理', id: '3' }],
      managerIdOptions: [],
      statusOptions: [{ fullName: '全款', id: '1' }, { fullName: '定金', id: '2' }, { fullName: '保证金', id: '3' }, { fullName: '余款', id: '4' }]
    }
  },
  computed: {},
  created() {},
  mounted() {
    this.getUserList()
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  watch: {
    // async 'dataForm.brand'() {
    //   if (this.campusOptions.length === 0) {
    //     try {
    //       this.campusLoading = true
    //       const res = await getCampusSelector({ brand: this.dataForm.brand, fields: 'id,fullName,enCode,status,provinceId,cityId,shortName' })
    //       const list = Array.isArray(res.data) ? res.data : []
    //       this.campusOptions = list.filter(i => i.status != 'close')
    //       if (this.dataForm.campusId) {
    //         let item = this.campusOptions.find(item => item.id === this.dataForm.campusId)
    //         item ? (this.openTime = this.custom.toDate(item.openDate, 'yyyy-MM-dd')) : ''
    //       }
    //     } catch (err) {
    //       console.error('加载校区下拉选项失败:', err)
    //       this.$message.error('校区选项加载失败')
    //       this.campusOptions = []
    //     } finally {
    //       this.campusLoading = false
    //     }
    //   } else {
    //     this.dataForm.campusId = undefined
    //     this.openTime = ''
    //   }
    // }
  },
  methods: {
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.managerIdOptions = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    goBack() {
      this.$emit('refresh')
    },
    async loadCampusOptions() {
      try {
        this.campusLoading = true
        const res = await getCampusSelector({ brand: this.dataForm.brand, fields: 'id,fullName,provinceId,cityId,shortName' })
        const list = Array.isArray(res.data) ? res.data : []
        this.campusOptions = list.filter(i => i.status != 'close')
      } catch (err) {
        console.error('加载校区下拉选项失败:', err)
        this.$message.error('校区选项加载失败')
        this.campusOptions = []
      } finally {
        this.campusLoading = false
      }
    },
    handleChangeCampus(value) {
      let campus = this.campusOptions.find(o => o.id == value)
      this.dataForm.area = [campus.provinceId, campus.cityId]
      this.dataForm.campusName = campus.shortName
    },
    init(id, isDetail, isEdit) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isEdit = isEdit || false
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/finance/receipt/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (!this.dataForm.id) {
            request({
              url: `/api/finance/receipt`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/finance/receipt/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
